package com.tansuo365.test1.service.kucunservice.kucunimpl;

import com.tansuo365.test1.bean.kucunbean.ChukuDetails;
import com.tansuo365.test1.bean.kucunbean.PosunDetails;
import com.tansuo365.test1.bean.kucunbean.PosunZong;
import com.tansuo365.test1.bean.kucunbean.WuliaoKucunRiZhi;
import com.tansuo365.test1.mapper.kucunmapper.PosunDetailsMapper;
import com.tansuo365.test1.mapper.kucunmapper.PosunZongMapper;
import com.tansuo365.test1.mapper.kucunmapper.WuliaoKucunRiZhiMapper;
import com.tansuo365.test1.mapper.kucunmapper.WuliaoKucunSearMapper;
import com.tansuo365.test1.service.kucunservice.PosunZongService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 已加入save时的各自的单据保存和物料主表的更改以及物料日志表的新增
 */
@Service
public class PosunZongServiceImpl implements PosunZongService {

    @Resource
    private PosunZongMapper posunZongMapper;
    @Resource
    private PosunDetailsMapper posunDetailsMapper;
    @Resource
    private WuliaoKucunSearMapper wuliaoKucunSearMapper;
    @Resource
    private WuliaoKucunRiZhiMapper wuliaoKucunRiZhiMapper;

    @Override
    public String getTodayMaxPosunDanNumber() {
        return posunZongMapper.getTodayMaxPosunDanNumber();
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return posunZongMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<PosunZong> listPosunZong(PosunZong posunZong) {
        return posunZongMapper.listPosunZong(posunZong);
    }

    @Override
    public int deleteBatchByPKArr(Integer[] ids) {
        return posunZongMapper.deleteBatchByPKArr(ids);
    }

    @Override
    public int insertSelective(PosunZong posunZong) {
        return posunZongMapper.insertSelective(posunZong);
    }

    @Override
    @Transactional
    public int saveZongAndDetails(PosunZong posunZong, List<PosunDetails> posunDetailsList) {
        int test = -100;
        for(PosunDetails chukuDetails : posunDetailsList){
            //查看物品在库存中是否不够出库数量
            String chukukufang = chukuDetails.getChukukufang();
            String mingchengguige = chukuDetails.getMingchengguige();
            Double chukushuliang = chukuDetails.getChukushuliang(); //实际是查看破损数量获取破损的出库数量
            Double jieyu = wuliaoKucunRiZhiMapper.checkOutTheNumberEnough(chukukufang, mingchengguige);
            if(chukushuliang>jieyu){ //其实这里的chukushuliang可以理解为posunshuliang
                return test;
            }
            test--;
        }


        int insertCode = 0;
        insertCode = posunZongMapper.insertSelective(posunZong);
        if(!(insertCode>0)){
            return 0;
        }
        Integer zongzhangId = posunZong.getId();
        int i = 0;
        int upKu = 0;
        int inSertRizhi = 0;
        int inDetails = 0;
        //保存每个破损单物料
        for(PosunDetails posunDetails : posunDetailsList){
            //设置对应总表id
            i++;
            posunDetails.setZongzhangid(zongzhangId);
            inDetails += posunDetailsMapper.insertSelective(posunDetails);
//            upKu += wuliaoKucunSearMapper.updateWuliaoPosunWeixiuByWuliaoName(posunDetails.getMingchengguige(), posunDetails.getChukushuliang(), posunDetails.getChukukufang());
            //TODO 将每个保存的详细insert到物料日志表
            //TODO 在保存到物料日志时需要加String type,表明是正常出库/破损出库/维修出库
            WuliaoKucunRiZhi wuliaoKucunRiZhi = new WuliaoKucunRiZhi();
            wuliaoKucunRiZhi.setMingchengguige(posunDetails.getMingchengguige());
            wuliaoKucunRiZhi.setXiangmuweihu(posunZong.getXiangmumingcheng());
            wuliaoKucunRiZhi.setPosunshuliang(posunDetails.getChukushuliang());
            wuliaoKucunRiZhi.setFachuleixing("破损出库");//实际是加到了破损数量中
            wuliaoKucunRiZhi.setDanwei(posunDetails.getDanwei());
            wuliaoKucunRiZhi.setRukukufang(posunDetails.getChukukufang());
            wuliaoKucunRiZhi.setWuliaobianma(posunDetails.getWuliaobianma());
            wuliaoKucunRiZhi.setDalei(posunDetails.getDalei());
            inSertRizhi += wuliaoKucunRiZhiMapper.insertSelective(wuliaoKucunRiZhi);
        }
        if(!(i==posunDetailsList.size() && i!=0)){
            return 0;
        }else if (!(i==inDetails)){
            //update的破损数和条目不一样多,那么就说明有的物料没执行update成功,或者是因为库存没有这个物料
            return -1;
        }
        if(!(inSertRizhi==posunDetailsList.size())){
            return 0;
        }
        return i; //返回的是插入破损详情物料的数量
    }

    @Override
    public PosunZong selectByPrimaryKey(Integer id) {
        return posunZongMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(PosunZong posunZong) {
        return posunZongMapper.updateByPrimaryKeySelective(posunZong);
    }
}
